package com.sujing.webService.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.jws.WebService;

import com.sujing.webService.HelloService;

@WebService(endpointInterface = "com.sujing.webService.HelloService")
public class HelloServiceImpl implements HelloService {

	@Override
	public String insert(String name, String pwd) {
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://127.0.0.1:3306/user";
		String user = "root";

		String password = "123456";

		try {
			Class.forName(driver);

			Connection conn = DriverManager.getConnection(url, user, password);

			if (!conn.isClosed())
				System.out.println("Succeeded connecting to the Database!");

			String sql = "insert into user(name,pwd)" + "values(?,?)";

			PreparedStatement pstmt = conn.prepareStatement(sql);

			pstmt.setString(1, name);
			pstmt.setString(2, pwd);
			int rows = pstmt.executeUpdate();
			System.out.println("-----------------");
			System.out.println(name + "\t" + pwd);
			System.out.println("-----------------");
			System.out.println("--------ROWS-------" + rows);
			pstmt.close();
			conn.close();
			return "{result:0}";
		} catch (ClassNotFoundException e) {

			System.out.println("Sorry,can`t find the Driver!");
			e.printStackTrace();
			return "Sorry,can`t find the Driver!";

		} catch (SQLException e) {

			return "{result:1}";

		}
	}

	public String delete(String name) {
		String Driver = "com.mysql.jdbc.Driver";
		String user = "root";
		String url = "jdbc:mysql://127.0.0.1:3306/user";
		String password = "123456";
		try {
			Class.forName(Driver);
			Connection conn = DriverManager.getConnection(url, user, password);
			if (!conn.isClosed())
				System.out.println("Succeeded connecting to the Database!");
			String sql = "delete from user where name=?";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, name);
			int rows = pstmt.executeUpdate();
			pstmt.close();
			conn.close();
			if (rows == 0) {
				return "{result:5}";
			} else {
				return "{result:0}";
			}
		} catch (ClassNotFoundException e) {

			System.out.println("sorry,can't find driver!");
		} catch (SQLException e) {
			return "{result:5}";
		}
		return "{result:5}";
	}

	public String select(String name,String pwd){
		String Driver="com.mysql.jdbc.Driver";
		String url="jdbc:mysql://127.0.0.1:3306/user";
		String user="root";
		String password="123456";
		
		try {
			Class.forName(Driver);
			Connection conn=DriverManager.getConnection(url, user, password);
			if(!conn.isClosed())
				System.out.println("Succeeded connecting to the Database!");
			String sql="select pwd from user where name=? ";
			
			PreparedStatement pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, name);
			ResultSet rs=pstmt.executeQuery();
			while(rs.next()){
				if(pwd.equals(rs.getString("pwd"))){
					return "{result:0}";
				}else{
					return "{result:6}";
				}
			}
			pstmt.close();
			conn.close();
			
		} catch (ClassNotFoundException  e) {
		System.out.println("sorry,can't find driver!");
		} catch (SQLException e) {
			return "{result:5}"; 
		}
		return "{result:5}";
	}

	public String update(String name, String pwd) {
		String Driver = "com.mysql.jdbc.Driver";
		String url="jdbc:mysql://127.0.0.1:3306/user";
		String user = "root";
		String password = "123456";
		try {
			Class.forName(Driver);
			Connection conn = DriverManager.getConnection(url, user, password);
			if (!conn.isClosed())
				System.out.println("Succeeded connecting to the Database!");
			String sql = "update user set pwd=? where name=?";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, pwd);
			pstmt.setString(2, name);
			int rows = pstmt.executeUpdate();
			pstmt.close();
			conn.close();
			if (rows == 0) {
				return "{result:5}";
			} else {
				return "{result:0}";
			}
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
		return "sorry,can't find driver";

	}

}
